package cn.web.demo.security;

import cn.web.demo.model.Permission;
import cn.web.demo.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    IPermissionService iPermissionService;
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.headers()
                .frameOptions().sameOrigin()
                .httpStrictTransportSecurity().disable();

        http.csrf().disable();

        List<Permission> list=iPermissionService.findAllPermission();
        for(Permission permission:list){
            if(!StringUtils.isEmpty(permission.getPath())){
                http.authorizeRequests().antMatchers(permission.getPath()).hasAnyAuthority(permission.getAuthorization_flag());
            }
        }

        http.authorizeRequests()

                .antMatchers("/login").permitAll()
                .antMatchers("/**").fullyAuthenticated()
                .and()
                .formLogin()
                .loginPage("/login");
                //.failureForwardUrl("/login-error");
    }



}
